﻿Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
Imports GameEx
Imports System.Runtime.InteropServices

Public Class ProcMessage

    Private _ResizeHandle As Boolean = False
    Public Property ResizeHandle() As Boolean
        Get
            Return _ResizeHandle
        End Get
        Set(ByVal value As Boolean)
            _ResizeHandle = value
        End Set
    End Property


    Dim mousex As Integer
    Dim mousey As Integer
    Dim drag As Boolean = False

    Public Const SWP_NOMOVE = 2
    Public Const SWP_NOSIZE = 1
    Public Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
    Public Const HWND_TOPMOST = -1
    Public Const HWND_NOTOPMOST = -2

    Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" _
           (ByVal hwnd As Long, _
           ByVal hWndInsertAfter As Long, _
           ByVal x As Long, _
           ByVal y As Long, _
           ByVal cx As Long, _
           ByVal cy As Long, _
           ByVal wFlags As Long) As Long

    Public Function SetTopMostWindow(hwnd As Long, Topmost As Boolean) _
         As Long

        If Topmost = True Then 'Make the window topmost
            SetTopMostWindow = SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, _
               0, FLAGS)
        Else
            SetTopMostWindow = SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, _
               0, 0, FLAGS)
            SetTopMostWindow = False
        End If
    End Function



    Private Sub SizeFormOnShown() Handles Me.Shown

        'Plugin.GEmvoy.WriteGVEntry(">> ProcMessage: Pos + Size on SHOWN.")
        'Plugin.GEmvoy.WriteGVEntry(">> ProcMessage: Size: " & Me.Width & ", " & Me.Height)
        'Plugin.GEmvoy.WriteGVEntry(">> ProcMessage: Pos: " & Me.Location.X & ", " & Me.Location.Y)

        Me.BringToFront()
        Me.TopMost = True
        ' SetTopMostWindow(Me.Handle, True)

        If ResizeHandle Then
            ResizePB.Show()
        End If

        If Not (IsOnScreen(Me)) Then
            Me.Location = New Point(10, 10)
            GEmvoy.Setts.MsgX = 10
            GEmvoy.Setts.MsgY = 10
            GEmvoy.Setts.SaveSetts()
        End If

        If Me.Width < 50 Or Me.Height < 30 Then
            Plugin.GEmvoy.WriteGVEntry(">> ProcMessage: Window smaller than that permitted. Use default size + save")
            Me.Size = New Size(250, 80)
            GEmvoy.Setts.MsgWidth = 250
            GEmvoy.Setts.MsgHeight = 80
            GEmvoy.Setts.SaveSetts()
        End If

        Me.BringToFront()
        Me.TopMost = True

        Plugin.GEmvoy.WriteGVEntry(">> ProcMessage: FINAL Pos + Size.")
        Plugin.GEmvoy.WriteGVEntry(">> ProcMessage: Size: " & Me.Width & ", " & Me.Height)
        Plugin.GEmvoy.WriteGVEntry(">> ProcMessage: Pos: " & Me.Location.X & ", " & Me.Location.Y)



    End Sub

    Private Sub ProcMessage_FormClosing(sender As Object, e As Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        TopmostTimer.Stop()
        Plugin.GEmvoy.WriteGVEntry(">> ProcMessage: Pos + Size on CLOSING.")
        Plugin.GEmvoy.WriteGVEntry(">> ProcMessage: Size: " & Me.Width & ", " & Me.Height)
        Plugin.GEmvoy.WriteGVEntry(">> ProcMessage: Pos: " & Me.Location.X & ", " & Me.Location.Y)
        GEmvoy.Setts.MsgHeight = Me.Height
        GEmvoy.Setts.MsgWidth = Me.Width
        GEmvoy.Setts.MsgX = Me.Location.X
        GEmvoy.Setts.MsgY = Me.Location.Y

    End Sub

    Private Sub ProcMessage_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        'Plugin.GEmvoy.WriteGVEntry("Position + Size on startup: ")
        'Plugin.GEmvoy.WriteGVEntry(">> ProcMessage: Size: " & GEmvoy.Setts.MsgWidth & ", " & GEmvoy.Setts.MsgHeight)
        'Plugin.GEmvoy.WriteGVEntry(">> ProcMessage: Pos: " & GEmvoy.Setts.MsgX & ", " & GEmvoy.Setts.MsgY)
        'Me.Font = GEmvoy.Setts.MsgFont
        'Me.Size = New Size(GEmvoy.Setts.MsgWidth, GEmvoy.Setts.MsgHeight)
        'Me.Location = New Point(GEmvoy.Setts.MsgX, GEmvoy.Setts.MsgY)
        Me.Opacity = 1
        Me.Font = GEmvoy.Setts.MsgFont
        Me.Size = New Size(GEmvoy.Setts.MsgWidth, GEmvoy.Setts.MsgHeight)
        Me.Location = New Point(GEmvoy.Setts.MsgX, GEmvoy.Setts.MsgY)

        Me.ForeColor = GEmvoy.Setts.MsgFGCol
        Me.BackColor = GEmvoy.Setts.MsgBGCol

        If GEmvoy.Setts.MsgImg <> "" Then
            LogoPB.Image = Image.FromFile(GEmvoy.Setts.MsgImg)
        End If

        If GEmvoy.Setts.MsgTicker = False Then
            Ticker.Image = Nothing
        End If

        'Dim lR As Long
        'Dim hWnd As IntPtr = Me.Handle
        'lR = SetTopMostWindow(hWnd, True)

        TopmostTimer.Start()

    End Sub

    Public Function IsOnScreen(ByVal form As Form) As Boolean
        Plugin.GEmvoy.WriteGVEntry(">> ProcMessage: Check Form on Screen....")
        Dim screens() As Screen = Screen.AllScreens
        For Each scrn As Screen In screens
            '   Plugin.GEmvoy.WriteGVEntry(">> ProcMessage: Screen X,Y: " & scrn.Bounds.X & ", " & scrn.Bounds.Y)
            '  Plugin.GEmvoy.WriteGVEntry(">> ProcMessage: Screen W,H: " & scrn.Bounds.Width & ", " & scrn.Bounds.Height)
            '  Plugin.GEmvoy.WriteGVEntry(">> ProcMessage: ------")
            Dim formRectangle As Rectangle = New Rectangle(form.Left, form.Top, form.Width, form.Height)
            If scrn.WorkingArea.Contains(formRectangle) Then
                Plugin.GEmvoy.WriteGVEntry(">> ProcMessage: Form in bounds of this screen. Return True.")
                Return True
            End If
        Next
        Plugin.GEmvoy.WriteGVEntry(">> ProcMessage: Form NOT in the bounds of any screen. Return False.")
        Return False
    End Function

    Public Sub ChangeMsg(msg As String, msgno As Integer)
        If msgno = 1 Then
            Msg1LB.Text = msg
        ElseIf msgno = 2 Then
            Msg2LB.Text = msg
        End If

        Me.Refresh()
        Me.BringToFront()
        Me.TopMost = True
    End Sub

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles LogoPB.MouseDown
        drag = True
        mousex = Windows.Forms.Cursor.Position.X - Me.Left
        mousey = Windows.Forms.Cursor.Position.Y - Me.Top
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles LogoPB.MouseMove
        ' Debug.WriteLine("Mouse Move")
        If drag Then
            Me.Top = Windows.Forms.Cursor.Position.Y - mousey
            Me.Left = Windows.Forms.Cursor.Position.X - mousex
        End If
    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles LogoPB.MouseUp
        '  Debug.WriteLine("Mouse Up")
        drag = False
        'My.Settings._Location = Me.Location
        'My.Settings.Save()
    End Sub

    Private Sub Resize_MD(sender As Object, e As EventArgs) Handles ResizePB.MouseDown, Ticker.MouseDown
        drag = True
        'bottom left co-ordinates:
        mousex = Me.Left
        mousey = Me.Top
    End Sub

    Private Sub Resize_MM(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ResizePB.MouseMove, Ticker.MouseMove
        ' Debug.WriteLine("Mouse Move")
        If drag Then
            Me.Width = Windows.Forms.Cursor.Position.X - mousex
            Me.Height = Windows.Forms.Cursor.Position.Y - mousey
        End If
    End Sub

    Private Sub Resize_MU(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ResizePB.MouseUp, Ticker.MouseUp
        drag = False
    End Sub



    Private Sub LogoPB_Click(sender As Object, e As MouseEventArgs) Handles LogoPB.Click

        If e.Button = Windows.Forms.MouseButtons.Right Then
            Me.Close()
        End If

        GEmvoy.Setts.MsgHeight = Me.Height
        GEmvoy.Setts.MsgWidth = Me.Width
        GEmvoy.Setts.MsgX = Me.Location.X
        GEmvoy.Setts.MsgY = Me.Location.Y

        My.Computer.Audio.PlaySystemSound(System.Media.SystemSounds.Asterisk)
        '  GameEx.Plugin.GEmvoy.WriteGVEntry("MSG: Width on click save: " & Me.Width)

    End Sub


    Public foreThread As UInteger = Nothing
    Public appThread As UInteger = Nothing

    <DllImport("user32.dll", SetLastError:=True)> _
    Private Shared Function GetWindowThreadProcessId(hWnd As IntPtr, ByRef lpdwProcessId As UInteger) As UInteger
    End Function

    ' When you don't want the ProcessId, use this overload and pass IntPtr.Zero for the second parameter
    <DllImport("user32.dll")> _
    Private Shared Function GetWindowThreadProcessId(hWnd As IntPtr, ProcessId As IntPtr) As UInteger
    End Function

    <DllImport("kernel32.dll")> _
    Private Shared Function GetCurrentThreadId() As UInteger
    End Function

    ''' <summary>The GetForegroundWindow function returns a handle to the foreground window.</summary>
    <DllImport("user32.dll")> _
    Private Shared Function GetForegroundWindow() As IntPtr
    End Function

    <DllImport("user32.dll")> _
    Private Shared Function AttachThreadInput(idAttach As UInteger, idAttachTo As UInteger, fAttach As Boolean) As Boolean
    End Function

    <DllImport("user32.dll", SetLastError:=True)> _
    Private Shared Function BringWindowToTop(hWnd As IntPtr) As Boolean
    End Function

    <DllImport("user32.dll", SetLastError:=True)> _
    Private Shared Function BringWindowToTop(hWnd As HandleRef) As Boolean
    End Function

    <DllImport("user32.dll")> _
    Private Shared Function ShowWindow(hWnd As IntPtr, nCmdShow As UInteger) As Boolean
    End Function

    Private Sub TopmostTimer_Tick(sender As Object, e As EventArgs) Handles TopmostTimer.Tick
        'Plugin.GEmvoy.WriteGVEntry(">> ProcMessage: Size: " & Me.Width & ", " & Me.Height)
        'Plugin.GEmvoy.WriteGVEntry(">> ProcMessage: Pos: " & Me.Location.X & ", " & Me.Location.Y)
        'Plugin.GEmvoy.WriteGVEntry("vISIBLE:" & Me.Visible.ToString)
        'Plugin.GEmvoy.WriteGVEntry("Opacity: " & Me.Opacity.ToString)
        'foreThread = GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero)
        'appThread = GetCurrentThreadId()
        'If GetForegroundWindow() <> Me.Handle Then
        '    Plugin.GEmvoy.WriteGVEntry(">> ProcMessage: Proc window NOT topmost")
        '    Me.BringToFront()
        '    Me.TopMost = True
        'Else
        'End If
    End Sub

    'Private Sub PushFormToTop(FormForTop As Form)
    '    'Debug.WriteLine(vbCr & "++++++++ Push Form Topmost Triggered +++++++++")
    '    'Debug.WriteLine("FORM: " & FormForTop.ToString)
    '    Try
    '        Dim HWND_TOPMOST As New IntPtr(-1)
    '        Dim SWP_NOACTIVATE As UInteger = &H10
    '        Dim SWP_NOMOVE As UInteger = &H2
    '        Dim SWP_NOSIZE As UInteger = &H1
    '        SetWindowPos(FormForTop.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_NOACTIVATE)
    '    Catch ex As Exception
    '        Plugin.GEmvoy.WriteGVEntry("Error whilst push for to top: " & ex.Message)
    '    End Try

    'End Sub

    'Private Sub KillHideOS()
    '    'kill any Backgrounds
    '    Dim bgProc() As Process = Process.GetProcessesByName("HideOS")
    '    For Each cpw As Process In bgProc
    '        cpw.Kill()
    '    Next
    'End Sub

    '<DllImport("user32.dll")> _
    'Public Shared Function SetWindowPos(hWnd As IntPtr, hWndInsertAfter As IntPtr, X As Integer, Y As Integer, cx As Integer, cy As Integer, _
    'uFlags As UInteger) As Boolean
    'End Function
    'Dim HWND_TOPMOST As New IntPtr(-1)
    'Dim SWP_NOACTIVATE As UInteger = &H10
    'Dim SWP_NOMOVE As UInteger = &H2
    'Dim SWP_NOSIZE As UInteger = &H1


End Class